<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="testcase-objects.html" />
<link rel="prev" href="legacy-unit-tests.html" />
<link rel="parent" href="module-unittest.html" />
<link rel="next" href="testcase-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>23.3.4 Classes and functions </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="23.3.3 re-using old test"
  href="legacy-unit-tests.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="23.3 unittest  "
  href="module-unittest.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="23.3.5 testcase Objects"
  href="testcase-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="legacy-unit-tests.html">23.3.3 Re-using old test</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-unittest.html">23.3 unittest  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="testcase-objects.html">23.3.5 TestCase Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0025340000000000000000"></a><a name="unittest-contents"></a>
<br>
23.3.4 Classes and functions
            
</h2>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-5005' xml:id='l2h-5005' class="class">TestCase</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>methodName</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Instances of the <tt class="class">TestCase</tt> class represent the smallest
  testable units in the <tt class="module">unittest</tt> universe.  This class is
  intended to be used as a base class, with specific tests being
  implemented by concrete subclasses.  This class implements the
  interface needed by the test runner to allow it to drive the
  test, and methods that the test code can use to check for and
  report various kinds of failure.

<p>
Each instance of <tt class="class">TestCase</tt> will run a single test method:
  the method named <var>methodName</var>.  If you remember, we had an
  earlier example that went something like this:

<p>
<div class="verbatim"><pre>
  def suite():
      suite = unittest.TestSuite()
      suite.addTest(WidgetTestCase('testDefaultSize'))
      suite.addTest(WidgetTestCase('testResize'))
      return suite
</pre></div>

<p>
Here, we create two instances of <tt class="class">WidgetTestCase</tt>, each of
  which runs a single test.

<p>
<var>methodName</var> defaults to <code>'runTest'</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-5006' xml:id='l2h-5006' class="class">FunctionTestCase</tt></b>(</nobr></td>
  <td><var>testFunc</var><big>[</big><var>,
                  setUp</var><big>[</big><var>, tearDown</var><big>[</big><var>, description</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  This class implements the portion of the <tt class="class">TestCase</tt> interface
  which allows the test runner to drive the test, but does not provide
  the methods which test code can use to check and report errors.
  This is used to create test cases using legacy test code, allowing
  it to be integrated into a <tt class="module"><a href="module-unittest.html">unittest</a></tt>-based test
  framework.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-5007' xml:id='l2h-5007' class="class">TestSuite</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>tests</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  This class represents an aggregation of individual tests cases and
  test suites.  The class presents the interface needed by the test
  runner to allow it to be run as any other test case.  Running a
  <tt class="class">TestSuite</tt> instance is the same as iterating over the suite,
  running each test individually.

<p>
If <var>tests</var> is given, it must be an iterable of individual test cases or
  other test suites that will be used to build the suite initially.
  Additional methods are provided to add test cases and suites to the
  collection later on.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-5008' xml:id='l2h-5008' class="class">TestLoader</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
  This class is responsible for loading tests according to various
  criteria and returning them wrapped in a <tt class="class">TestSuite</tt>.
  It can load all tests within a given module or <tt class="class">TestCase</tt>
  subclass.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-5009' xml:id='l2h-5009' class="class">TestResult</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
  This class is used to compile information about which tests have succeeded
  and which have failed.
</dl>

<p>
<dl><dt><b><tt id='l2h-5010' xml:id='l2h-5010'>defaultTestLoader</tt></b></dt>
<dd>
  Instance of the <tt class="class">TestLoader</tt> class intended to be shared.  If no
  customization of the <tt class="class">TestLoader</tt> is needed, this instance can
  be used instead of repeatedly creating new instances.
</dd></dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-5011' xml:id='l2h-5011' class="class">TextTestRunner</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>stream</var><big>[</big><var>,
                  descriptions</var><big>[</big><var>, verbosity</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  A basic test runner implementation which prints results on standard
  error.  It has a few configurable parameters, but is essentially
  very simple.  Graphical applications which run test suites should
  provide alternate implementations.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5012' xml:id='l2h-5012' class="function">main</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>module</var><big>[</big><var>,
                 defaultTest</var><big>[</big><var>, argv</var><big>[</big><var>,
                 testRunner</var><big>[</big><var>, testLoader</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  A command-line program that runs a set of tests; this is primarily
  for making test modules conveniently executable.  The simplest use
  for this function is to include the following line at the end of a
  test script:

<p>
<div class="verbatim"><pre>
if __name__ == '__main__':
    unittest.main()
</pre></div>
</dl>

<p>
In some cases, the existing tests may have been written using the
<tt class="module"><a href="module-doctest.html">doctest</a></tt> module.  If so, that module provides a 
<tt class="class">DocTestSuite</tt> class that can automatically build
<tt class="class">unittest.TestSuite</tt> instances from the existing
<tt class="module">doctest</tt>-based tests.

<span class="versionnote">New in version 2.3.</span>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="23.3.3 re-using old test"
  href="legacy-unit-tests.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="23.3 unittest  "
  href="module-unittest.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="23.3.5 testcase Objects"
  href="testcase-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="legacy-unit-tests.html">23.3.3 Re-using old test</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-unittest.html">23.3 unittest  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="testcase-objects.html">23.3.5 TestCase Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
